<?php

namespace App\Jobs;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use OpenAI;
use Rainsens\Dinglog\Facades\Dinglog;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use App\Models\DeepNew;
use DOMDocument;
use Nesk\Puphpeteer\Puppeteer;

class TouchNewsJob implements ShouldQueue
{
	use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
	
	public $model;

	public $timeout = 400;

	public function __construct($model)
	{
		$this->model = $model;
	}
	
	public function handle()
    {

    	$model = $this->model;
    	
    	$this->huxiuCollecton($model->url);
    		
    	
	}

	public function huxiuCollecton($url)
	{
		ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)');

    	$puppeteer = new Puppeteer; // 新建 Puppeteer 实例

         $browser = $puppeteer->launch(['args'=>['--no-sandbox']]); // 启动无头浏览器
         $page = $browser->newPage(); // 打开新的标签页
       
         $page->tryCatch->goto($url,[
             'timeout' => 0,
             'read_timeout' => 30

         ]); // 访问页面
        	
       	 
         $html = $page->content();
       
        
        /* return $html; // 返回 js 渲染后的页面
        
		$html = Browsershot::url($baseUrl1)->setDelay(1000)->bodyHtml();*/
		
		

    	//$html = '<a href="aaaa"><div class="content-item"><div>测试</div></div></a>';
    	$dom = new DOMDocument();
    	

    	libxml_use_internal_errors(true);
    	//$html = htmlspecialchars($html);
    	
		
        $dom->loadHTML('<?xml encoding="UTF-8">' . $html);

         $data = [
        	'collections'=>0,
        	'likes'=>0
        ];

        if($this->model->source=='huxiu'){
        	 $iElements =  $dom->getElementsByTagName('i');

	        $ik = 0;
	   		foreach($iElements as $iElement){
	   			
	   			if ($iElement->getAttribute('class')=='num') {
	   				
	   				if($ik==0){

	   					$data['collections'] = (int)$iElement->textContent;

	   				}elseif($ik==2){

	   					$data['likes'] =  (int)$iElement->textContent;
	   				}

	   				$ik++;
	   				
	   			}
	   			
	   		}


        }elseif($this->model->source=="36ke"){

        	$divElements =  $dom->getElementsByTagName('div');
	   		foreach($divElements as $divElement){
	   			

	   			if (strpos($divElement->getAttribute('class'), 'thumbNum') !== false) {
	   				$data['likes'] =(int)$divElement->textContent;
	   			}
	   			
	   		}

	   		$bElements =  $dom->getElementsByTagName('b');
	   		foreach($bElements as $bElement){
	   			

	   			if (strpos($bElement->getAttribute('class'), 'favorite-count') !== false) {
	   				$data['collections'] = (int)$bElement->textContent;
	   			}
	   			
	   		}
        }

       
   		
   		$this->model->update($data);
   		$browser->close();
	}
}
